package com.icss.clothes.mapper;

import com.icss.clothes.entity.Address;
import com.icss.clothes.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

//登录，通过用户名去查找用户信息
@Mapper
public interface UserMapper {

    //用于修改用户信息时查找是否有相同用户名
    @Select("select * from user where user_name=#{user_name}")
    public User selectUserByUname(String user_name);
    @Insert("insert into user(user_name,realname,upwd,email,uphone,ucreatetime)" +
            " values(#{user_name},#{realname},#{upwd},#{email},#{uphone},now())")
    public void insertIntoUser(User user);



 // 修改用户信息
    @Update("<script>" +
            "update user set user_id=#{user_id}" +
            "<if test='user_name!=null and user_name!=\"\"'> , user_name=#{user_name}</if>" +
            "<if test='upwd!=null and upwd!=\"\"'> , upwd=#{upwd}</if>" +
            "<if test='email!=null and email!=\"\"'> , email=#{email}</if>" +
            "<if test='uphone!=null and uphone!=\"\"'> , uphone=#{uphone}</if>" +
            "  where user_id=#{user_id}"+
            "</script>")
    public void updateUser(User user);

    //通过用户id获取用户名
    @Select("select user_name from user where user_id=#{user_id}")
    public String selectUnameByUid(int user_id);
    //从数据库中通过指定的用户编号查询得到所有收货地址信息
    @Select("select a.*,u.email,u.uphone from address a join user u  on u.user_id=a.user_id where a.user_id=#{user_id}")
    public List<Address> selectAllByUser_id(int user_id);
}
